﻿namespace Standards.Data.EntityFramework.Configurations
{
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.ModelConfiguration;
    using Standards.Model.Models;

    public class PermissionsConfiguration : EntityTypeConfiguration<PermissionDto>
    {
        public PermissionsConfiguration()
        {
            this.ToTable("Permissions");

            this.Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // EF5 only supports enums when targetting .NET 4.5 which is only possible in VS2012
            // Even then, it only supports storing enums as numbers, so we create another dummy property
            // on our entity to allow us to store the permission as a string
            // See this feature request http://goo.gl/PO8X5
            this.Ignore(p => p.Permission);
            this.Property(p => p.PermissionAsString).HasColumnName("Permission").HasMaxLength(100).IsRequired();

            this.HasMany(p => p.Roles).WithMany(r => r.Permissions).Map(cfg =>
                {
                    cfg.ToTable("RolePermissions");
                    cfg.MapLeftKey("RoleId");
                    cfg.MapRightKey("PermissionId");
                });
        }
    }
}